<template>
  <el-dialog
    title="编辑活动券"
    :visible.sync="visible"
    width="600px"
    :close-on-click-modal="false"
    class="dialog-container"
    @close="handlerClose"
  >
    <el-form ref="ticketCouponForm" :model="formData" :rules="rules" size="small" label-width="106px" label-suffix="：">
      <el-form-item label="关联活动">
        <span>{{ ticketInfo.title }} - {{ ticketInfo.issue_no }}</span>
      </el-form-item>
      <el-form-item label="目标用户">
        <span><target-user-type :row="ticketInfo" /></span>
      </el-form-item>
      <el-form-item v-if="ticketInfo.target_user_type === 1" label="" prop="ext_data.start_time">
        <!-- 目标用户为新用户 start-->
        <div class="ext-data-line">
          <span>在 </span>
          <el-date-picker
            v-model="formData.ext_data.start_time"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择注册日期"
            align="right"
            :picker-options="pickerOptions"
          />
          <span> 后注册的用户为新用户</span>
        </div>
        <!-- 目标用户为新用户 end-->
      </el-form-item>
      <el-form-item v-else-if="ticketInfo.target_user_type === 2" label="" prop="ext_data.day">
        <!-- 目标用户为购买用户 start-->
        <div class="ext-data-line">
          <span>距用户上一次购买 </span>
          <el-input-number v-model="formData.ext_data.day" />
          <span> 天</span>
        </div>
        <!-- 目标用户为购买用户 end-->
      </el-form-item>
      <el-form-item v-else-if="ticketInfo.target_user_type === 3" label="" prop="ext_data.silent">
        <!-- 目标用户为沉默用户 start-->
        <div class="ext-data-line">
          <span>截止 </span>
          <el-date-picker
            v-model="formData.ext_data.start_time"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期"
            align="right"
            :picker-options="pickerOptions"
          />
          <span> ,用户已有</span>
          <el-input-number v-model="formData.ext_data.day" />
          <span> 天未登录</span>
        </div>
        <!-- 目标用户为沉默用户 end-->
      </el-form-item>
      <div class="row-title">活动券设置</div>
      <el-form-item label="卡券类型" prop="coupon_type">
        <el-radio-group v-model="formData.coupon_type">
          <el-radio :label="1">优惠券</el-radio>
          <el-radio :label="2">打折卡</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="活动券期号" prop="issue_no">
        <el-input v-model="formData.issue_no" />
      </el-form-item>
      <el-form-item v-if="formData.coupon_type === 1" label="满减设置" prop="preferential">
        <div>
          <span>满 </span>
          <el-input-number v-model="formData.maxprice" :min="0.01" :step="0.1" :precision="2" />
          元，<span> 减 </span>
          <el-input-number v-model="formData.price" :min="0.01" :max="formData.maxprice" :step="0.1" :precision="2" />
          元
        </div>
      </el-form-item>
      <el-form-item v-if="formData.coupon_type === 2" label="折扣" prop="discount">
        <el-input-number v-model="formData.discount" :min="0.01" :step="0.1" :max="9.99" :precision="2" />
      </el-form-item>
      <el-form-item label="领取时间" prop="time">
        <el-date-picker
          v-model="formData.time"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item label="有效天数" prop="effective_day">
        <el-input-number v-model="formData.effective_day" :min="0" :step="1" />
        <div class="edu-tips">0 表示永久有效</div>
      </el-form-item>
    </el-form>
    <form-footer
      slot="footer"
      :loading="confirmLoading"
      class="dialog-footer"
      @cancel="handlerClose"
      @submit="handlerConfirm"
    />
  </el-dialog>
</template>

<script>
import FormFooter from '@/components/form-footer'
import TargetUserType from '../../components/targetUserType'

export default {
  name: 'UpdatedDialog',
  components: {
    FormFooter,
    TargetUserType
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    ticketInfo: {
      type: Object,
      required: true
    },
    dataInfo: {
      type: Object,
      required: true
    }
  },
  data() {
    const extData = this.dataInfo.ext_data
    if (extData.start_time) {
      extData.start_time = new Date(extData.start_time * 1000)
    }

    return {
      searchTicketLoading: false,
      confirmLoading: false,
      formData: {
        coupon_type: 1,
        maxprice: undefined,
        price: undefined,
        effective_day: 0,
        // time: '',
        discount: undefined,
        ...this.dataInfo,
        ext_data: extData,
        time: this.dataInfo.derive_stime ? [new Date(this.dataInfo.derive_stime), new Date(this.dataInfo.derive_etime)] : ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '现在',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      rules: {
        issue_no: [
          { required: true, message: '请输入活动期号', trigger: ['blur', 'change'] }
        ],
        coupon_type: [
          { required: true, message: '请选择卡券类型', trigger: ['blur', 'change'] }
        ],
        preferential: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (!this.$util.isDef(this.formData.maxprice)) {
                callback(new Error('请输入满减条件'))
              } else if (!this.$util.isDef(this.formData.price)) {
                callback(new Error('请输入满减价格'))
              }
              callback()
            }
          }
        ],
        discount: [
          { required: true, message: '请输入折扣信息', trigger: ['blur', 'change'] }
        ],
        time: [{ required: true, message: '请选择领取时间', trigger: ['blur', 'change'] }],
        ext_data: {
          start_time: [{ required: true, message: '请选择时间', trigger: ['blur', 'change'] }],
          day: [{ required: true, message: '请输入天数', trigger: ['blur', 'change'] }],
          silent: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!this.$util.isDef(this.formData.ext_data.start_time)) {
                  callback(new Error('请选择时间'))
                } else if (!this.$util.isDef(this.formData.ext_data.day)) {
                  callback(new Error('请输入天数'))
                }
                callback()
              }
            }
          ]
        }
      }
    }
  },
  watch: {
    'formData.coupon_type': {
      handler() {
        this.$refs.ticketCouponForm.clearValidate()
      }
    }
  },
  methods: {
    // 关闭后重置字段
    handlerClose() {
      this.$emit('update:visible', false);
    },
    // 保存活动
    handlerConfirm() {
      this.$refs.ticketCouponForm.validate((valid) => {
        if (valid) {
          // this.confirmLoading = true
          this.$http.put(
              '/admin/market/ticket/' + this.ticketInfo.id + '/coupon/' + this.dataInfo.id + '/edit',
              this.formData,
              ({ msg }) => {
                this.$message.success(msg)
                this.confirmLoading = false
                this.handlerClose()
                this.$emit('reload');
              },
              ({ msg }) => {
                this.$message.error(msg)
                this.confirmLoading = false
              }
          )
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-container {
  .ext-data-line {
    margin-left: -82px;
  }

  .row-title {
    font-size: 14px;
    font-weight: bold;
    color: #303133;
    margin-bottom: 10px;
  }
}
</style>
